<?php
/**
 * @author jim
 * @date 2017/11/25
 */

namespace app\admin\controller;


use app\admin\model\Company;

class Townscompany extends Common
{
        public function index() {

            if (session('company_type') > 3) { // 惠民店用户不可进入
                // 无权进入
                $this->redirect('index/index');
            }

            $where = array();
            $whereStart = array();
            $whereEnd   = array();

            // 公司条件
            $whereCountyComCondition = array();

            // 定义存储查询条件的数组
            $conditionArr = array();

            // 基本的查询条件
            if (session('province_id') > 0) {
                $where['com.ProvinceId'] = session('province_id');
                $whereCountyComCondition['ProvinceId'] = session('province_id');
            }

            if (session('city_id') > 0) {
                $where['com.CityId'] = session('city_id');
                $whereCountyComCondition['CityId'] = session('city_id');
            }

            if (session('county_id') > 0) {
                $where['com.CountyId'] = session('county_id');
                $whereCountyComCondition['CountyId'] = session('county_id');
            }

            $whereCountyComCondition['CompanyTypeId'] = 3;

            $where['com.CompanyTypeId'] = 4;

            // 列表查询条件
            if (input('sCountyId') && input('sCountyId')>0) {
                $conditionArr['sCountyId'] = input('sCountyId');
                $where['com.CountyId'] = input('sCountyId');
                $this->assign('sCountyId',input('sCountyId'));

                // 获取相关的区县数据
                $companyModel = new Company();
                $townCompanyData = $companyModel->selectData(array('CountyId'=>input('sCountyId'),'CompanyTypeId'=>4),'CompanyName,TownId');

                $this->assign('townCompanyData',$townCompanyData);
            }

            if (input('sTownId') && input('sTownId')>0) {
                $conditionArr['sTownId'] = input('sTownId');
                $where['com.TownId'] = input('sTownId');
                $this->assign('sTownId',input('sTownId'));
            }


            if (input('county_id')) {
                $conditionArr['county_id'] = input('county_id');
                $where['com.CountyId'] = input('county_id');
            }

            if (input('sTrueName')) {
                $conditionArr['sTrueName'] = input('sTrueName');
                $where['c.TrueName'] = ['like',input('sTrueName').'%'];
                $this->assign('sTrueName',input('sTrueName'));
            }

            if (input('sAddress')) {
                $conditionArr['sAddress'] = input('sAddress');
                $where['com.address'] = ['like',input('sAddress').'%'];
                $this->assign('sAddress',input('sAddress'));
            }

            $this->assign('start',input('start'));
            $this->assign('end',input('end'));
            if (input('start')) {
                $conditionArr['start'] = input('start');
                $whereStart['com.CreateOn'] = ['>',input('start').' 00:00'];
            }

            if (input('end')) {
                $conditionArr['end'] = input('end');
                $whereEnd['com.CreateOn'] = ['<',input('end').' 23:59'];
            }


            $pagesize = config('default_page_size');
            $res = $this->db_app->table('company')
                ->alias('com')
                ->join('customer c','com.CustomerId = c.Id','LEFT')
                ->where($where)
                ->where($whereStart)
                ->where($whereEnd)
                ->order('com.Money desc')
                ->field('com.Id,com.CompanyName,com.address,com.Money,com.LeftMoney,com.CreateOn,c.TrueName,com.ProvinceId,com.CityId,com.CountyId,com.TownId')
                ->paginate($pagesize,false,[
                    'query' => $conditionArr
                ]); // 防止参数丢失


            $page = $res->render(); // 分页
            $data = $res->all(); // 数据

            $this->assign('page',$page);
            $this->assign('data',$data);

            // 获取相关的公司列表
            $companyModel = new Company();
            $countyCompanyData = $companyModel->selectData($whereCountyComCondition,'CompanyName,CountyId');
            $this->assign('countyCompanyData',$countyCompanyData);

            return $this->fetch();
        }

    // 获取惠民店
    function ajaxGetTownCompany() {
        if (request()->isAjax()) {
            $outData = array('data'=>'','code'=>'','message'=>'');//输出参数
            $countyId = input('post.countyId');
            // 获取相关的区县数据
            $companyModel = new Company();
            $townCompanyData = $companyModel->selectData(array('CountyId'=>$countyId,'CompanyTypeId'=>4),'CompanyName,TownId');
            if ($townCompanyData) {
                $outData['code'] = 1;
                $outData['message'] = "获取成功";
                $outData['data'] = $townCompanyData;
            } else {
                $outData['code'] = 2;
                $outData['message'] = "获取失败";
            }

            return $outData;
        }
    }

}